package com.saifan.wyy_ov.utils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.model.interfaces.IViewOnClick;
import com.saifan.wyy_ov.ui.view.MaterialDialog;
import com.saifan.wyy_ov.ui.view.Picker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author Wangli
 * @Description: 选择日期和时间的控件
 * @date: 2015/5/11 16:38
 * @version: V1.0
 */
public class DateTimePickUtils {

    private static MaterialDialog dialog = null;
    private static Picker year, month, day, hour, minute;
    private static TextView title_edit;
    private static List<String> yearList;
    private static List<String> monthList;
    private static List<String> dayList;
    private static List<String> hourList;
    private static List<String> minuteList;
    private static Calendar calendar;

    //返回允许选择过去的时间
    public static void showAllowTimeout(final Activity context, final IViewOnClick viewOnClick, final Date date  ) {
        yearList = new ArrayList<String>();
        monthList = new ArrayList<String>();
        dayList = new ArrayList<String>();

        LinearLayout dateTimeLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.picker_date, null);
        year = (Picker) dateTimeLayout.findViewById(R.id.year);
        month = (Picker) dateTimeLayout.findViewById(R.id.month);
        day = (Picker) dateTimeLayout.findViewById(R.id.day);

        title_edit = (TextView) dateTimeLayout.findViewById(R.id.title);
        calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        final int year_init = calendar.get(Calendar.YEAR);
        final int month_init = calendar.get(Calendar.MONTH);
        final int day_init = calendar.get(Calendar.DAY_OF_MONTH);
//                int hour_init = calendar.get(Calendar.HOUR_OF_DAY);
//                int minute_init = calendar.get(Calendar.MINUTE);



        //年
        for (int i = year_init-5;  i < year_init ; i++) {
            yearList.add(String.valueOf(i));
        }
        for (int i = year_init; i <= year_init + 5; i++) {
            yearList.add(String.valueOf(i));
        }
        //月
        for (int i = 1; i <= 12; i++) {
            monthList.add(String.valueOf(i));
        }
        //日
        for (int i = 1; i <= calendar.getActualMaximum(Calendar.DATE); i++) {
            dayList.add(String.valueOf(i));
        }

        year.setData(yearList);
        year.setSelected(String.valueOf(year_init));
        month.setData(monthList);
        month.setSelected(String.valueOf(month_init + 1));
        day.setData(dayList);
        day.setSelected(String.valueOf(day_init));


        year.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.YEAR, Integer.valueOf(text));
                changeDay();
                changeTitle();
            }
        });
        month.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.MONTH, Integer.valueOf(text) - 1);
                changeDay();
                changeTitle();
            }
        });
        day.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(text));
                changeTitle();
            }
        });

        dialog = new MaterialDialog(context);
        title_edit.setText(formatTime(calendar));
        dialog.setCanceledOnTouchOutside(true);
        dialog.setView(dateTimeLayout);
        dialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                viewOnClick.onClick(formatTime(calendar));

            }
        });
        dialog.show();

    }



    public static void show(final Activity context, final IViewOnClick viewOnClick, Date date) {
        yearList = new ArrayList<String>();
        monthList = new ArrayList<String>();
        dayList = new ArrayList<String>();
        hourList = new ArrayList<String>();
        minuteList = new ArrayList<String>();
        LinearLayout dateTimeLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.picker_datetime, null);
        year = (Picker) dateTimeLayout.findViewById(R.id.year);
        month = (Picker) dateTimeLayout.findViewById(R.id.month);
        day = (Picker) dateTimeLayout.findViewById(R.id.day);
        hour = (Picker) dateTimeLayout.findViewById(R.id.hour);
        minute = (Picker) dateTimeLayout.findViewById(R.id.minute);
        title_edit = (TextView) dateTimeLayout.findViewById(R.id.title);
        calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int year_init = calendar.get(Calendar.YEAR);
        int month_init = calendar.get(Calendar.MONTH);
        int day_init = calendar.get(Calendar.DAY_OF_MONTH);
        int hour_init = calendar.get(Calendar.HOUR_OF_DAY);
        int minute_init = calendar.get(Calendar.MINUTE);


        //年
        for (int i = year_init; i <= year_init + 5; i++) {
            yearList.add(String.valueOf(i));
        }

        //月
        for (int i = 1; i <= 12; i++) {
            monthList.add(String.valueOf(i));
        }
        //日
        for (int i = 1; i <= calendar.getActualMaximum(Calendar.DATE); i++) {
            dayList.add(String.valueOf(i));
        }
        //时
        for (int i = 0; i <= 23; i++) {
            hourList.add(String.valueOf(i));
        }
        //分
        for (int i = 0; i <= 59; i++) {
            minuteList.add(String.valueOf(i));
        }
        year.setData(yearList);
        year.setSelected(String.valueOf(year_init));
        month.setData(monthList);
        month.setSelected(String.valueOf(month_init + 1));
        day.setData(dayList);
        day.setSelected(String.valueOf(day_init));
        hour.setData(hourList);
        hour.setSelected(String.valueOf(hour_init));
        minute.setData(minuteList);
        minute.setSelected(String.valueOf(minute_init));

        year.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {

                changeDay(Integer.valueOf(text),calendar.get(Calendar.MONTH));
                changeTitle();
            }
        });
        month.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                changeDay(calendar.get(Calendar.YEAR),Integer.valueOf(text) - 1);
                changeTitle();
            }
        });
        day.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(text));
                changeTitle();
            }
        });
        hour.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(text));
                changeTitle();
            }
        });
        minute.setOnSelectListener(new Picker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                calendar.set(Calendar.MINUTE, Integer.valueOf(text));
                changeTitle();
            }
        });
        dialog = new MaterialDialog(context);
        title_edit.setText(formatTime(calendar));
        dialog.setCanceledOnTouchOutside(true);
        dialog.setView(dateTimeLayout);
        dialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(calendar.getTime().after(new Date())){
                    dialog.dismiss();
                    viewOnClick.onClick(formatTime(calendar));
                }else {
                    ToastUtil.showToast(context,"不能小于当前时间");
                }


            }
        });
        dialog.show();

    }
    private static String formatTime(Calendar calendar) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sd.format(calendar.getTime());
    }

    //改变天
    private static void changeDay() {
        dayList.clear();
        //日
        for (int i = 1; i <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            dayList.add(String.valueOf(i));
        }
        day.setData(dayList);
        day.setSelected(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
    }

    //改变天
    private static void changeDay(int y ,int m) {
        dayList.clear();
        //日
        int maxday= getDaysInMonth(y,m);
        if(calendar.get(Calendar.DAY_OF_MONTH)>maxday){
            calendar.set(Calendar.DAY_OF_MONTH,maxday);
        }
        for (int i = 1; i <=maxday; i++) {
            dayList.add(String.valueOf(i));
        }
        day.setData(dayList);
        day.setSelected(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        calendar.set(Calendar.MONTH, m);
        calendar.set(Calendar.YEAR, y);
    }

    //改变对话框标题
    private static void changeTitle() {

        title_edit.setText(formatTime(calendar));
    }


    public static int getDaysInMonth(  int year,int month) {
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.MARCH:
            case Calendar.MAY:
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.OCTOBER:
            case Calendar.DECEMBER:
                return 31;
            case Calendar.APRIL:
            case Calendar.JUNE:
            case Calendar.SEPTEMBER:
            case Calendar.NOVEMBER:
                return 30;
            case Calendar.FEBRUARY:
             return    ((GregorianCalendar)calendar).isLeapYear(year)?29:28;
//                return (year%4==0&&year%100!=0||year%400==0) ? 29 : 28;
            default:
                throw new IllegalArgumentException("Invalid Month");
        }
    }
}
